package com.apes.framework.plugin.condition.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.condition.model.AccessSequences;
import com.apes.framework.plugin.condition.repository.AccessSequencesItemRepository;
import com.apes.framework.plugin.condition.repository.AccessSequencesRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 存取顺序
 *
 * @author bieb
 * @create 2018-11-13 10:58:22
 */
@Service
public class AccessSequencesService extends DomainService {

    @Autowired
    AccessSequencesRepository  accessSequencesRepository;
    @Autowired
    AccessSequencesItemRepository accessSequencesItemRepository;


    /***
     * 功能： 创建
     * @param request
     * @return
     */
    public AccessSequences create(SimpleRequest request) {
        AccessSequences accessSequences = request.getO(AccessSequences.class);
        return accessSequencesRepository.saveAndFlush(accessSequences);
    }

    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public AccessSequences update(SimpleRequest request) {
        checkData(request);
        AccessSequences accessSequences = request.getO(AccessSequences.class);
        return accessSequencesRepository.saveAndFlush(accessSequences);
    }

    /***
     * 功能： 删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        checkData(request);
        AccessSequences conditionField = request.getO(AccessSequences.class);
        accessSequencesRepository.delete(conditionField);
        return "删除成功";
    }

    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        AccessSequences oldConditionField = accessSequencesRepository.findOne(id);
        if (oldConditionField == null) throw new RuntimeException("存储顺序【" + id + "】在系统中不存在。");
    }
}